/**
 * Created by MetaLab_Gu on 2016/4/15.
 */
angular.module('metalabPartnerApp').controller('TechListAllCtrl', ['$scope', '$state', '$stateParams', 'MetaResources', 'MetaSEO', 'MetaRemind',
    function ($scope, $state, $stateParams, resource, MetaSEO, MetaRemind) {
        if ($state.is('meta.technology.list.all')) $scope.tabs[0].active = true;
        else if ($state.is('meta.technology.list.released')) $scope.tabs[1].active = true;

        $scope.waiting = true;

        ///**数据模板
        $scope.page = {
            currentPage: $stateParams.page,
            data: [],
            total: 0,
            pageNumber: 10
        };

        /**
         * @function 请求数据
         * @param page 页码
         * @param area 【筛选】领域
         * @param effect 【筛选】效果
         * @param origin 【筛选】来源
         * @param keywords 关键字
         */
        var changeData = function (page, area, effect, origin, keywords) {
            $scope.waiting = true;
            if (keywords) {
                $scope.waitingPromise = resource.search.website({
                    searchText: keywords,
                    type: "technology",
                    effectTags: effect,
                    areas: area,
                    origin: origin === '国外' ? undefined : origin,
                    origin_not: origin === '国外' ? '中国' : undefined,
                    _range: ((parseInt(page) - 1) * $scope.page.pageNumber + 1) + '_' + parseInt(page) * $scope.page.pageNumber,
                    online: "true",
                    version: "online",
                    verified: "true",
                    _sort: "-rank,-updatedAt"
                }, function (data, headers) {
                    $scope.waiting = false;
                    $scope.page.total = headers()['x-count'] ? parseInt(headers()['x-count']) : 0;
                    $scope.page.data = data;
                }, function (err) {
                    $scope.waiting = false;
                    $scope.page.total = 0;
                    $scope.page.data = [];
                    MetaRemind.error('出错了', err);
                }).$promise;
            } else {
                $scope.waitingPromise = resource.technology.query({
                    keywords: keywords,
                    effectTags: effect,
                    areas: area,
                    origin: origin === '国外' ? undefined : origin,
                    origin_not: origin === '国外' ? '中国' : undefined,
                    _range: ((parseInt(page) - 1) * $scope.page.pageNumber + 1) + '_' + parseInt(page) * $scope.page.pageNumber,
                    version: 'online',
                    online: true,
                    verified: true,
                    _sort: '-rank,-updatedAt'
                }, function (data, headers) {
                    $scope.waiting = false;
                    $scope.page.total = headers()['x-count'] ? parseInt(headers()['x-count']) : 0;
                    $scope.page.data = data;
                }, function (err) {
                    $scope.waiting = false;
                    $scope.page.total = 0;
                    $scope.page.data = [];
                    MetaRemind.error('出错了', err);
                }).$promise;
            }
        };

        /**
         * @function 请求数据
         */
        $scope.filters[0].config.chose = $stateParams.area || '不限';
        $scope.filters[1].config.chose = $stateParams.effect || '不限';
        $scope.filters[2].config.chose = $stateParams.origin || '不限';
        $scope.$parent.searchText = $stateParams.keywords;
        changeData($stateParams.page, $stateParams.area, $stateParams.effect, $stateParams.origin, $stateParams.keywords);
        MetaSEO.setSEO('迈科技-技术方案推广-科技成果转化',
            '迈科技-集中展示通过迈科技研究院认证的技术方案，根据行业领域，应用效果等标签，筛选你想要的技术方案，免费获取技术方的联系信息，主动寻找解决方案', $stateParams.area + ',' + $stateParams.keywords + ',', $stateParams.origin + ',' + $stateParams.effect);
    }]);
